<template>
  <view>
    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-form ref="formRef" :model="form" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公告标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入公告标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公告类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择公告类型">
                <el-option v-for="dict in notice_type_dict" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in status_dict" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
                <mavon-editor v-model="form.content"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </view>
</template>
<script setup>
import { status_dict, notice_type_dict } from "@/constant/dict";
import { inject } from "vue";
const createOne = inject('createOne');
const updateOne = inject('updateOne');
const emit = defineEmits(["success"]);
const module = "公告";
const open = ref(false);
const title = ref("");
const form = ref({
  title: "",
  type: "",
  status:1,
  content: "",
});
// 新建弹窗
function doCreate() {
  title.value = "新建" + module;
  open.value = true;
}
// 更新弹窗
function doUpdate(data) {
  form.value = data;
  title.value = "修改" + module;
  open.value = true;
}
// 表单提交
function submitForm(){
  // 如果ID为空 新建
  if(form.value.id === undefined){
    createOne(form.value).then(res=>{
     // console.log(res)
      open.value = false;
      emit('success')
    })
  }else{
    updateOne(form.value).then(res=>{
     // console.log(res)
      open.value = false;
      emit('success')
    })
  }
}
defineExpose({
  doCreate,
  doUpdate,
});
</script>
